package org.xtext.ic.dsl.generator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.xtext.ic.dsl.IcDslExtensions;
import org.xtext.ic.dsl.icDsl.Connection;
import org.xtext.ic.dsl.icDsl.CreateDAO;
import org.xtext.ic.dsl.icDsl.Entity;
import org.xtext.ic.dsl.icDsl.Layer;
import org.xtext.ic.dsl.icDsl.Model;
import org.xtext.ic.dsl.icDsl.PackagePrefix;

@SuppressWarnings("all")
public class HibernateAnnotationsDAOExtensions extends IcDslExtensions {
  public CharSequence baseDAO(final Model m, final Connection c, final CreateDAO cdao) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, cdao);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _importConnection = this.importConnection(m, c);
    _builder.append(_importConnection, "");
    _builder.newLineIfNotEmpty();
    _builder.append("import java.util.List;");
    _builder.newLine();
    _builder.append("import java.util.ArrayList;");
    _builder.newLine();
    _builder.append("import java.lang.reflect.ParameterizedType;");
    _builder.newLine();
    _builder.append("import java.lang.reflect.Type;");
    _builder.newLine();
    _builder.append("import org.hibernate.Criteria;");
    _builder.newLine();
    _builder.append("import org.hibernate.HibernateException;");
    _builder.newLine();
    _builder.append("import org.hibernate.Session;");
    _builder.newLine();
    _builder.append("import org.hibernate.Transaction;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public abstract class BaseDAO<T> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public Class getGenericClass() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Class result = null;");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Type type = this.getClass().getGenericSuperclass();");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if(type instanceof ParameterizedType){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("ParameterizedType pt = (ParameterizedType) type;");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Type[] fieldArgTypes = pt.getActualTypeArguments();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("result = (Class) fieldArgTypes[0];");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return result;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean cadastrar(T entity) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("boolean saved = false;");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Session session = FabricaSessao.getSessao();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (session != null) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Transaction t = session.beginTransaction();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("try{");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("session.save(entity);");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.commit();");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("saved = true;");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("System.out.println(\"Problemas ao inserir: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.rollback();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("} finally{");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("FabricaSessao.fechar(session);");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return saved;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public List<T> procurar(T entity) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("List<T> list = new ArrayList<T>();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Class genericClass = getGenericClass();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Session session = FabricaSessao.getSessao();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (session != null) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Transaction t = session.beginTransaction();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("try{");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("Criteria c = session.createCriteria(genericClass);");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("list = c.list();");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.commit();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("System.out.println(\"Problemas ao procurar: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.rollback();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("} finally{");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("FabricaSessao.fechar(session);");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return list;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean atualizar(T entity) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("boolean saved = false;");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Session session = FabricaSessao.getSessao();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (session != null) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Transaction t = session.beginTransaction();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("try{");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("session.update(entity);");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.commit();");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("saved = true;");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("System.out.println(\"Problemas ao atualizar: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.rollback();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("} finally{");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("FabricaSessao.fechar(session);");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return saved;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean excluir(T entity) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("boolean saved = false;");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Session session = FabricaSessao.getSessao();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (session != null) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Transaction t = session.beginTransaction();");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("try{");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("session.delete(entity);");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.commit();");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("saved = true;");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("System.out.println(\"Problemas ao deletar: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("t.rollback();");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("} finally{");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("FabricaSessao.fechar(session);");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return saved;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence entityDAO(final Model m, final CreateDAO cdao, final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, cdao);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _importEntity = this.importEntity(m, e);
    _builder.append(_importEntity, "");
    _builder.newLineIfNotEmpty();
    _builder.append("import java.util.List;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public interface ");
    String _name = e.getName();
    _builder.append(_name, "");
    _builder.append("DAO {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean cadastrar(");
    String _name_1 = e.getName();
    _builder.append(_name_1, "	");
    _builder.append(" entity);");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public List<");
    String _name_2 = e.getName();
    _builder.append(_name_2, "	");
    _builder.append("> procurar(");
    String _name_3 = e.getName();
    _builder.append(_name_3, "	");
    _builder.append(" entity);");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean atualizar(");
    String _name_4 = e.getName();
    _builder.append(_name_4, "	");
    _builder.append(" entity);");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public boolean excluir(");
    String _name_5 = e.getName();
    _builder.append(_name_5, "	");
    _builder.append(" entity);");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence entityDAOImpl(final Model m, final Connection c, final CreateDAO cdao, final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, cdao);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.newLine();
    CharSequence _importEntity = this.importEntity(m, e);
    _builder.append(_importEntity, "");
    _builder.newLineIfNotEmpty();
    {
      boolean _isBasedao = cdao.isBasedao();
      boolean _not = (!_isBasedao);
      if (_not) {
        CharSequence _importConnection = this.importConnection(m, c);
        _builder.append(_importConnection, "");
      }
    }
    _builder.newLineIfNotEmpty();
    _builder.append("import java.util.List;");
    _builder.newLine();
    {
      boolean _isBasedao_1 = cdao.isBasedao();
      boolean _not_1 = (!_isBasedao_1);
      if (_not_1) {
        _builder.append("import java.util.ArrayList;");
        _builder.newLine();
        _builder.append("import org.hibernate.Criteria;");
        _builder.newLine();
        _builder.append("import org.hibernate.HibernateException;");
        _builder.newLine();
        _builder.append("import org.hibernate.Session;");
        _builder.newLine();
        _builder.append("import org.hibernate.Transaction;");
        _builder.newLine();
      }
    }
    _builder.newLine();
    _builder.append("public class ");
    String _name = e.getName();
    _builder.append(_name, "");
    _builder.append("DAOImpl");
    {
      boolean _isBasedao_2 = cdao.isBasedao();
      if (_isBasedao_2) {
        _builder.append(" extends BaseDAO<");
        String _name_1 = e.getName();
        _builder.append(_name_1, "");
        _builder.append(">");
      }
    }
    _builder.append(" implements ");
    String _name_2 = e.getName();
    _builder.append(_name_2, "");
    _builder.append("DAO {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    {
      boolean _isBasedao_3 = cdao.isBasedao();
      if (_isBasedao_3) {
        _builder.append("\t");
        _builder.append("public ");
        String _name_3 = e.getName();
        _builder.append(_name_3, "	");
        _builder.append("DAOImpl(){");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("super();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean cadastrar(");
        String _name_4 = e.getName();
        _builder.append(_name_4, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return super.cadastrar(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public List<");
        String _name_5 = e.getName();
        _builder.append(_name_5, "	");
        _builder.append("> procurar(");
        String _name_6 = e.getName();
        _builder.append(_name_6, "	");
        _builder.append(" entity) {\t\t\t");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return super.procurar(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean atualizar(");
        String _name_7 = e.getName();
        _builder.append(_name_7, "	");
        _builder.append(" entity) {\t\t\t");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return super.atualizar(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean excluir(");
        String _name_8 = e.getName();
        _builder.append(_name_8, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return super.excluir(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
      } else {
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean cadastrar(");
        String _name_9 = e.getName();
        _builder.append(_name_9, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("boolean saved = false;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("Session session = FabricaSessao.getSessao();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("if (session != null) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("Transaction t = session.beginTransaction();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("try{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("session.save(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.commit();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("saved = true;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} catch (HibernateException ex) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("System.out.println(\"Problemas ao inserir: \" + ex.getMessage());");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.rollback();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} finally{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("FabricaSessao.fechar(session);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return saved;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public List<");
        String _name_10 = e.getName();
        _builder.append(_name_10, "	");
        _builder.append("> procurar(");
        String _name_11 = e.getName();
        _builder.append(_name_11, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("List<");
        String _name_12 = e.getName();
        _builder.append(_name_12, "		");
        _builder.append("> list = new ArrayList<");
        String _name_13 = e.getName();
        _builder.append(_name_13, "		");
        _builder.append(">();");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("Session session = FabricaSessao.getSessao();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("if (session != null) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("Transaction t = session.beginTransaction();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("try{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("Criteria c = session.createCriteria(");
        String _name_14 = e.getName();
        _builder.append(_name_14, "				");
        _builder.append(".class);");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("list = c.list();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.commit();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} catch (HibernateException ex) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("System.out.println(\"Problemas ao procurar: \" + ex.getMessage());");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.rollback();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} finally{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("FabricaSessao.fechar(session);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return list;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean atualizar(");
        String _name_15 = e.getName();
        _builder.append(_name_15, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("boolean saved = false;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("Session session = FabricaSessao.getSessao();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("if (session != null) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("Transaction t = session.beginTransaction();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("try{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("session.update(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.commit();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("saved = true;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} catch (HibernateException ex) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("System.out.println(\"Problemas ao atualizar: \" + ex.getMessage());");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.rollback();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} finally{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("FabricaSessao.fechar(session);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return saved;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("@Override");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public boolean excluir(");
        String _name_16 = e.getName();
        _builder.append(_name_16, "	");
        _builder.append(" entity) {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("boolean saved = false;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("Session session = FabricaSessao.getSessao();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("if (session != null) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("Transaction t = session.beginTransaction();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("try{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("session.delete(entity);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.commit();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("saved = true;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} catch (HibernateException ex) {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("System.out.println(\"Problemas ao deletar: \" + ex.getMessage());");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("t.rollback();");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("} finally{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t\t");
        _builder.append("FabricaSessao.fechar(session);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return saved;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
      }
    }
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence importConnection(final Model m, final Connection c) {
    StringConcatenation _builder = new StringConcatenation();
    {
      EObject _eContainer = c.eContainer();
      if ((_eContainer instanceof Layer)) {
        _builder.append("import ");
        PackagePrefix _package = m.getPackage();
        String _packageName = this.packageName(_package, c);
        _builder.append(_packageName, "");
        _builder.append(".FabricaSessao;");
        _builder.newLineIfNotEmpty();
      } else {
        {
          PackagePrefix _package_1 = m.getPackage();
          String _name = _package_1.getName();
          boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(_name);
          boolean _not = (!_isNullOrEmpty);
          if (_not) {
            _builder.append("import ");
            PackagePrefix _package_2 = m.getPackage();
            String _name_1 = _package_2.getName();
            _builder.append(_name_1, "");
            _builder.append(".FabricaSessao;");
            _builder.newLineIfNotEmpty();
          } else {
            _builder.append("import FabricaSessao;");
            _builder.newLine();
          }
        }
      }
    }
    return _builder;
  }
}
